<template>
  <div>
    <!-- <div class="run_info">
      <div>延时统计</div>
    </div> -->
    <div id="chart">
      <Empty v-if="isEmpty" class="is_empty" :image="simpleImage" />
    </div>
    <div class="run_info">
      <div>每日运行详情</div>
      <div>
        <Select
          v-model:value="selected"
          :options="sysMonthOptions"
          style="width: 120px"
          @change="onChange"
        ></Select>
      </div>
    </div>
    <div id="lineChart">
      <Empty v-if="isEmpty2" class="is_empty" :image="simpleImage" />
    </div>
  </div>
</template>

<script setup lang="tsx">
import { computed, nextTick, ref } from 'vue';
import { useDicts } from '@boeyo/portal';
import { BoTable, BoTableProps } from '@boeyo/ui';
import { Empty, Select, message } from 'ant-design-vue';
import * as echarts from 'echarts';
import 'echarts/theme/macarons';
import { dayDelay, delayStatistics } from '../api';

const simpleImage = Empty.PRESENTED_IMAGE_SIMPLE;
const isEmpty = ref(false);
const isEmpty2 = ref(false);
const props = defineProps({
  stationId: {
    type: String,
    default: '',
  },
});
const { stationId } = props;
const selected = ref();
const sysMonthOptions = ref([]);

const getInfo = async () => {
  try {
    const res: any = await delayStatistics({ stationId });
    if (res.code !== 200 || !res.data || !res.data.length) {
      isEmpty.value = true;
      isEmpty2.value = true;
      message.info('暂无延时数据!');
      return;
    }

    sysMonthOptions.value = res.data.map((i) => {
      return {
        label: i.date,
        value: i.date,
      };
    });
    selected.value =
      sysMonthOptions.value[sysMonthOptions.value.length - 1]?.value;

    nextTick(() => {
      init(res.data);
      getLineInfo();
    });
  } catch (error) {
    isEmpty.value = true;
    isEmpty2.value = true;
  }
};
getInfo();
const getLineInfo = async () => {
  const result: any = await dayDelay({
    stationId,
    date: selected.value,
  });
  nextTick(() => {
    lineInit(result.data);
  });
};
const onChange = () => {
  getLineInfo();
};
let chart;
let lineChart;
const init = (data: any) => {
  const xData = data.map((i) => i.date);
  const seriesData = data.map((i) => Number(i.rate));
  if (chart) chart.clear();
  const node = document.querySelector('#chart');
  chart = echarts.init(node);
  const option = {
    grid: {
      top: '10%',
      left: '5%',
      right: '5%',
      bottom: '15%',
    },
    color: ['#40A5FD'],
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'shadow',
      },
    },
    xAxis: {
      type: 'category',
      // boundaryGap: [0, 0.01],
      splitLine: {
        show: false,
      },
      triggerEvent: true,
      axisLine: {
        show: true,
        lineStyle: {
          color: 'rgba(255,255,255,0.26)',
          type: 'dashed',
        },
      },
      axisLabel: {
        color: '#fff',
        interval: 0,
        // formatter: function (value) {
        //   if (value.length > 4) {
        //     // 假设超过4个字符就显示省略号
        //     return value.slice(0, 4) + '...'; // 显示前5个字符并添加省略号
        //   }
        //   return value;
        // },
      },

      data: xData,
    },
    yAxis: {
      name: '近十五天延时均值',
      nameTextStyle: {
        color: '#fff',
      },
      type: 'value',
      // min: 0,
      axisLabel: {
        show: true,
        color: '#fff',
      },
      axisTick: {
        show: false,
      },
      splitLine: {
        //show: false,
        lineStyle: {
          color: 'rgba(255,255,255,0.26)',
          type: 'dashed',
        },
      },
      axisLine: {
        show: false,
      },
    },
    series: [
      {
        type: 'bar',
        data: seriesData,
        barWidth: 30,
        itemStyle: {
          // barBorderRadius: 7.5,
        },
      },
    ],
  };
  chart.setOption(option);
  addEventListener('resize', function () {
    chart.resize();
  });
};
const lineInit = (data: any) => {
  const xData = data.map((i) => i.time);
  const seriesData = data.map((i) => i.avg);
  if (lineChart) lineChart.clear();
  const node = document.querySelector('#lineChart');
  lineChart = echarts.init(node);
  const option = {
    tooltip: {
      trigger: 'axis',
      axisPointer: {
        type: 'cross',
        label: {
          backgroundColor: '#d4e5ff',
        },
      },
    },
    grid: {
      left: '3%',
      right: '4%',
      bottom: '3%',
      containLabel: true,
    },
    xAxis: [
      {
        type: 'category',
        boundaryGap: false,
        data: xData,
        axisLabel: {
          show: true,
          color: '#fff',
        },
      },
    ],
    yAxis: [
      {
        name: '延时ms',
        nameTextStyle: {
          color: '#fff',
        },
        type: 'value',
        splitLine: {
          //show: false,
          lineStyle: {
            color: 'rgba(255,255,255,0.26)',
            type: 'dashed',
          },
        },
        axisLabel: {
          show: true,
          color: '#fff',
        },
      },
    ],
    series: [
      {
        type: 'line',
        stack: 'Total',
        areaStyle: {},
        emphasis: {
          focus: 'series',
        },
        itemStyle: {
          color: '#40A5FD',
        },
        data: seriesData,
      },
    ],
  };
  lineChart.setOption(option);
  addEventListener('resize', function () {
    lineChart.resize();
  });
};
</script>

<style lang="scss" scoped>
#chart,
#lineChart {
  width: 100%;
  height: 30vh;
}
.run_info {
  display: flex;
  justify-content: space-between;
  padding: 15px;
  & > div {
    font-weight: 700;
  }
}
</style>
